<script setup lang="ts">
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import axios from 'axios'
import ajax from "../main.ts";

const router = useRouter()
const username = ref('')
const password = ref('')
const isLoading = ref(false)

const handleLogin = async () => {
  if (!username.value || !password.value) {
    ElMessage({
      message: '请输入账号和密码',
      type: 'warning',
      duration: 2000
    })
    return
  }

  isLoading.value = true
  try {
    const response = await ajax.post('toLogin/', {
      username: username.value,
      password: password.value
    })

    if (response.data.code === 200) {
      ElMessage({
        message: '登录成功',
        type: 'success',
        duration: 2000
      })
      localStorage.setItem('token', response.data.token)
      localStorage.setItem('userid', response.data.id)
      localStorage.setItem('username', response.data.username)
      router.push('/')
    } else {
      ElMessage({
        message: response.data.msg || '登录失败，请检查账号密码',
        type: 'error',
        duration: 2000
      })
    }
  } catch (error) {
    ElMessage({
      message: '登录失败，请稍后重试',
      type: 'error',
      duration: 2000
    })
    console.error('登录失败:', error)
  } finally {
    isLoading.value = false
  }
}
</script>

<template>
  <div class="login-container">
    <div class="background-animation">
      <div class="circles">
        <div class="circle circle-1"></div>
        <div class="circle circle-2"></div>
        <div class="circle circle-3"></div>
      </div>
      <div class="grid-lines">
        <div class="grid-line grid-line-1"></div>
        <div class="grid-line grid-line-2"></div>
        <div class="grid-line grid-line-3"></div>
        <div class="grid-line grid-line-4"></div>
      </div>
      <div class="floating-dots">
        <div class="dot dot-1"></div>
        <div class="dot dot-2"></div>
        <div class="dot dot-3"></div>
        <div class="dot dot-4"></div>
        <div class="dot dot-5"></div>
      </div>
    </div>
    <div class="login-box">
      <div class="login-header">
        <h1>MCP CLIENT 登录</h1>
        <div class="tech-line"></div>
      </div>

      <div class="login-form">
        <div class="input-group">
          <span class="input-icon">👤</span>
          <input
            v-model="username"
            type="text"
            placeholder="请输入账号"
            class="input-field"
          >
          <div class="input-line"></div>
        </div>

        <div class="input-group">
          <span class="input-icon">🔒</span>
          <input
            v-model="password"
            type="password"
            placeholder="请输入密码"
            class="input-field"
          >
          <div class="input-line"></div>
        </div>

        <button
          class="login-button"
          :class="{ 'loading': isLoading }"
          @click="handleLogin"
          :disabled="isLoading"
        >
          <span v-if="!isLoading">登 录</span>
          <span v-else class="loading-spinner"></span>
        </button>
      </div>
    </div>
  </div>
</template>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: radial-gradient(
    circle at center,
    rgba(245, 243, 255, 0.1) 0%,
    rgba(233, 213, 255, 0.2) 25%,
    rgba(221, 214, 254, 0.3) 50%,
    rgba(199, 210, 254, 0.4) 75%,
    rgba(199, 210, 254, 0.5) 100%
  );
  background-attachment: fixed;
  position: relative;
  overflow: hidden;
}

.background-animation {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 0;
  overflow: hidden;
}

/* 圆形动画 */
.circles {
  position: absolute;
  width: 100%;
  height: 100%;
}

.circle {
  position: absolute;
  border-radius: 50%;
  filter: blur(40px);
}

.circle-1 {
  width: 300px;
  height: 300px;
  top: -100px;
  left: -100px;
  background: radial-gradient(
    circle at center,
    rgba(245, 243, 255, 0.2) 0%,
    rgba(233, 213, 255, 0.3) 100%
  );
  animation: float 15s ease-in-out infinite;
}

.circle-2 {
  width: 400px;
  height: 400px;
  bottom: -150px;
  right: -150px;
  background: radial-gradient(
    circle at center,
    rgba(221, 214, 254, 0.2) 0%,
    rgba(199, 210, 254, 0.3) 100%
  );
  animation: float 20s ease-in-out infinite reverse;
}

.circle-3 {
  width: 250px;
  height: 250px;
  top: 50%;
  left: 50%;
  background: radial-gradient(
    circle at center,
    rgba(245, 243, 255, 0.2) 0%,
    rgba(221, 214, 254, 0.3) 100%
  );
  transform: translate(-50%, -50%);
  animation: pulse 8s ease-in-out infinite;
}

/* 网格线动画 */
.grid-lines {
  position: absolute;
  width: 100%;
  height: 100%;
  background-image:
    linear-gradient(rgba(221, 214, 254, 0.15) 1px, transparent 1px),
    linear-gradient(90deg, rgba(199, 210, 254, 0.15) 1px, transparent 1px);
  background-size: 50px 50px;
  animation: gridMove 20s linear infinite;
}

.grid-line {
  position: absolute;
  background: linear-gradient(90deg,
    transparent,
    rgba(221, 214, 254, 0.3),
    rgba(199, 210, 254, 0.3),
    transparent
  );
  height: 1px;
  width: 100%;
}

.grid-line-1 { top: 20%; animation: moveLine 8s linear infinite; }
.grid-line-2 { top: 40%; animation: moveLine 12s linear infinite reverse; }
.grid-line-3 { top: 60%; animation: moveLine 10s linear infinite; }
.grid-line-4 { top: 80%; animation: moveLine 15s linear infinite reverse; }

/* 浮动点动画 */
.floating-dots {
  position: absolute;
  width: 100%;
  height: 100%;
}

.dot {
  position: absolute;
  width: 4px;
  height: 4px;
  border-radius: 50%;
  filter: blur(1px);
}

.dot-1 {
  background: rgba(245, 243, 255, 0.4);
  top: 15%;
  left: 20%;
  animation: floatDot 8s ease-in-out infinite;
}

.dot-2 {
  background: rgba(233, 213, 255, 0.4);
  top: 30%;
  right: 25%;
  animation: floatDot 12s ease-in-out infinite reverse;
}

.dot-3 {
  background: rgba(221, 214, 254, 0.4);
  top: 60%;
  left: 15%;
  animation: floatDot 10s ease-in-out infinite;
}

.dot-4 {
  background: rgba(199, 210, 254, 0.4);
  top: 75%;
  right: 20%;
  animation: floatDot 15s ease-in-out infinite reverse;
}

.dot-5 {
  background: rgba(245, 243, 255, 0.4);
  top: 45%;
  left: 50%;
  animation: floatDot 9s ease-in-out infinite;
}

/* 动画关键帧 */
@keyframes float {
  0%, 100% { transform: translate(0, 0); }
  25% { transform: translate(20px, 20px); }
  50% { transform: translate(0, 40px); }
  75% { transform: translate(-20px, 20px); }
}

@keyframes pulse {
  0%, 100% { transform: translate(-50%, -50%) scale(1); opacity: 0.3; }
  50% { transform: translate(-50%, -50%) scale(1.2); opacity: 0.5; }
}

@keyframes gridMove {
  0% { background-position: 0 0; }
  100% { background-position: 50px 50px; }
}

@keyframes moveLine {
  0% { transform: translateX(-100%); }
  100% { transform: translateX(100%); }
}

@keyframes floatDot {
  0%, 100% { transform: translate(0, 0); }
  25% { transform: translate(10px, -10px); }
  50% { transform: translate(0, -20px); }
  75% { transform: translate(-10px, -10px); }
}

.login-box {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  border-radius: 15px;
  padding: 40px;
  width: 400px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.2);
  position: relative;
  z-index: 1;
}

.login-header {
  text-align: center;
  margin-bottom: 40px;
}

.login-header h1 {
  color: #333;
  font-size: 28px;
  margin-bottom: 15px;
  font-weight: 800;
  background: linear-gradient(90deg, #6366f1, #8b5cf6);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  color: transparent;
}

.tech-line {
  height: 2px;
  width: 220px;
  background: linear-gradient(90deg, transparent, #6366f1, #8b5cf6, transparent);
  margin: 0 auto;
}

.input-group {
  margin-bottom: 30px;
  position: relative;
  display: flex;
  align-items: center;
}

.input-icon {
  font-size: 20px;
  margin-right: 12px;
  opacity: 0.7;
  transition: opacity 0.3s ease;
}

.input-field:focus + .input-line + .input-icon {
  opacity: 1;
}

.input-field {
  flex: 1;
  padding: 12px 0;
  background: transparent;
  border: none;
  color: #333;
  font-size: 16px;
  outline: none;
}

.input-field::placeholder {
  color: rgba(51, 51, 51, 0.5);
}

.input-line {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 1px;
  background: rgba(99, 102, 241, 0.2);
  transition: all 0.3s ease;
}

.input-field:focus + .input-line {
  background: linear-gradient(90deg, transparent, #6366f1, #8b5cf6, transparent);
  height: 2px;
}

.login-button {
  width: 100%;
  padding: 12px;
  background: linear-gradient(90deg, #6366f1, #8b5cf6);
  border: none;
  border-radius: 25px;
  color: #fff;
  font-size: 16px;
  cursor: pointer;
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;
  box-shadow: 0 4px 12px rgba(99, 102, 241, 0.3);
}

.login-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 16px rgba(99, 102, 241, 0.4);
  background: linear-gradient(90deg, #818cf8, #9333ea);
}

.login-button:active {
  transform: translateY(1px);
  box-shadow: 0 2px 8px rgba(99, 102, 241, 0.2);
  background: linear-gradient(90deg, #4f46e5, #7c3aed);
}

.login-button:disabled {
  opacity: 0.7;
  cursor: not-allowed;
}

.login-button::before {
  content: "";
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(
    90deg,
    rgba(255, 255, 255, 0) 0%,
    rgba(255, 255, 255, 0.2) 50%,
    rgba(255, 255, 255, 0) 100%
  );
  transition: left 0.8s;
}

.login-button:hover::before {
  left: 100%;
}

.loading-spinner {
  display: inline-block;
  width: 20px;
  height: 20px;
  border: 2px solid rgba(255, 255, 255, 0.3);
  border-radius: 50%;
  border-top-color: #fff;
  animation: spin 1s ease-in-out infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* 添加新的渐变层 */
.background-animation::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: radial-gradient(
    circle at 50% 50%,
    rgba(245, 243, 255, 0.1) 0%,
    rgba(233, 213, 255, 0.2) 25%,
    rgba(221, 214, 254, 0.3) 50%,
    rgba(199, 210, 254, 0.4) 75%,
    rgba(199, 210, 254, 0.5) 100%
  );
  animation: gradientPulse 15s ease-in-out infinite;
}

@keyframes gradientPulse {
  0%, 100% { opacity: 0.5; }
  50% { opacity: 0.8; }
}
</style>